/*************************************************************************
 *
 * SensorTask
 * 
 * (c) Ales Povalac, alpov@alpov.net, http://www.alpov.net/
 * 
 *************************************************************************
 *
 * $Id: sensor.c 63 2006-07-27 17:15:50Z alpov $
 * 
 *************************************************************************/         

#include <string.h>
#include <avr/io.h>
#include "../avrx/avrx.h"
#include "../avrx/AvrXFifo.h"
#include "sensor.h"
#include "1wire.h"
#include "data.h"
#include "serial.h"

TimerControlBlock SensorDelayTimer;
Mutex SensorReady;


/*************************************************************************
 * uloha SensorTask
 * min. stack fixme
 *************************************************************************/ 
AVRX_GCC_TASKDEF(SensorTask, 60, 3)
{
    int i;
    char addr[8];
    int countdown = 0;
    
    while (1) {
        AvrXStartTimer(&SensorDelayTimer, SENSOR_DELAY);
        if (!countdown) {
            Serial_RWDATA_W(ITEM_RTC, ADDR_BROADCAST);
            countdown = 60000 / SENSOR_DELAY;
        }
        
        OWConvertAll(1);
        for (i = 0; i < AvrXReadEEProm(&eedata.sensor_count); i++) {
            ReadEEPromBlock(addr, &eedata.sensors[i], sizeof(addr));
            OWReadTemperature(addr, &stdata.temperature[i]);
        }        
        AvrXSetSemaphore(&SensorReady);
        Serial_STDATA_W(ITEM_ALL);
        
        AvrXWaitTimer(&SensorDelayTimer);
        countdown--;
    }
}


/*************************************************************************
 * inicializace tasku
 *************************************************************************/ 
void InitSensorTask(void)
{
//  memset(SensorTaskStk, 0xA0A0, sizeof(SensorTaskStk));
    OWReset();
}

